package xhwl.insurance.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.ContextCompat;


/**
 * Created by admin on 2017/5/31.
 */

public class PermisionUtils {

    public static AlertDialog alertDialog = null;

    //安卓6.0版本以上请求权限的问题
    public static boolean islacksOfPermission(Context context, String permission) {
            return ContextCompat.checkSelfPermission(context, permission) ==
                    PackageManager.PERMISSION_DENIED;
    }

    /**
     * 获取本应用系统详情设置界面
     *
     * @param context
     * @return
     */
    public static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }

    /**
     * 此dialog应用于6.0版本以上获取权限被拒绝时弹出
     *
     * @param context
     */
    public static void dialog(final Context context, String alertmessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("设置权限");
        builder.setMessage("您未授予系统" + "\"" + alertmessage + "\"" + " 使用权限,请选择 \"权限管理\" 并赋予相应权限"); //设置内容
        builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                context.startActivity(getAppDetailSettingIntent(context));
            }
        });
        builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog = builder.create();
        alertDialog.show();
    }
}
